package com.spring.demo.propagation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @version 1.0.0
 * @Date: 2022/6/1 14:56
 * @Author ZhuYouBin
 * @Description
 */
@Component
public class ServiceA {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private ServiceB serviceB;

    @Transactional // 开启事务
    public void methodA() {
        System.out.println("1、调用ServiceA类中的methodA方法......");
        this.addMoney();
        System.out.println("2、调用serviceB.methodB()方法......");
        try {
            serviceB.methodB();
        } catch (Exception e) {
        }
        System.out.println("执行成功......");
    }

    // 模拟存钱功能
    public void addMoney() {
        // A用户存入 520 元
        String sql = "UPDATE spring.t_money SET balance = balance + 520  WHERE id = 10001";
        int update = jdbcTemplate.update(sql);
        System.out.println("A用户存入520元成功: " + update);
    }

}
